/**
 * Created by li on 2017/1/28.
 */
function Controller(map,fourGrid){

    this.map = map;
    this.fourGrid = fourGrid;

    var self = this;
    document.onkeydown = function(event){

        if(!global.isFalling){
            // alert(2)
            // // 此方块结束，下一个方块
            var four2 = new FourGrid('Z',3,1);
            self.map.addFourGrid(four2);
            new Controller(self.map,four2);
            // this.constructor(self.map,four2);
            return false;
        }

        switch(event.keyCode){
            //左
            case 37:

                //检测是否能向左移动

                var flag = self.map.canIMove(self.fourGrid,"left");
                if(flag){
                    self.map.rePaintWithfourGrid(self.fourGrid, true);
                    self.fourGrid.x -= 1;
                    self.map.rePaintWithfourGrid(self.fourGrid);
                }


                break;
            //右
            case 39:
                if(self.map.canIMove(self.fourGrid,"right")){
                    self.map.rePaintWithfourGrid(self.fourGrid,true);
                    self.fourGrid.x += 1;
                    self.map.rePaintWithfourGrid(self.fourGrid);
                }
                break;
            //下，
            case 40:
                var flag = self.map.canIMove(self.fourGrid,"down");
                // if (flag=="isFalling") {
                //     alert(1)
                //     self.fourGrid.isFalling = 2
                // }else

                if(flag){
                    self.map.rePaintWithfourGrid(self.fourGrid,true);
                    self.fourGrid.y -= 1;
                    self.map.rePaintWithfourGrid(self.fourGrid);
                    // console.log(self.fourGrid.y)
                }
                break;
            //上，旋转
            case 38:

                self.map.rePaintWithfourGrid(self.fourGrid,true);
                self.fourGrid.transform();
                self.map.rePaintWithfourGrid(self.fourGrid);

                break;

        }
    }

    if(!global.isFalling){
        //创建四格
    }else{
        //下落
    }
    //
    //start:function(){
    //
    //}

}
Controller.prototype = {
    constructor : Controller,
    startGame : function(){
        var self = this;
        setInterval(function(){

        },1);
    },
}